//
//  TrashView.swift
//  memo
//
//  Created by DaleGuo on 2023/10/16.
//

import SwiftUI

struct TrashView: View {
    @EnvironmentObject var homeVM: HomeViewModel
    
    @State var select: Bool = false
    
    @State var selected_list: [MemoEntity] = []
    
    private let columns: [GridItem] = [
        // 两列
        GridItem(.flexible(), spacing: 0),
        GridItem(.flexible(), spacing: 0),
    ]
    
    @State var offset_memo: CGSize = .zero
    
    var body: some View {
        // 构造一个网格
        NavigationStack {
            ScrollView {
                LazyVGrid(columns: columns, alignment: .center, spacing: 1, pinnedViews: [], content: {
                    ForEach(homeVM.trashMemos) { memo in
                        VStack(alignment: .leading, spacing: 2) {
                            Text(memo.title)
                                .lineLimit(1)
                                .font(.title3)
                                .fontWeight(.bold)
                            
                            Text(memo.content.prefix(10) + "...")
                            
                            Spacer()
                            
                            Text(memo.updated_date.formatted())
                                .font(.caption)
                                .foregroundStyle(Color("SecondaryText"))
                        }
                        .frame(maxWidth: .infinity, alignment: .center)
                        .padding()
                        .background(Color.theme.background)
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                        .padding(5)
                        .overlay(alignment: .topTrailing) {
                            if select {
                                Image(systemName: isMemoInDel_list(memo: memo) ? "checkmark.circle" : "circle")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(width: 25)
                                    .offset()
                                    .shadow(color: Color.black, radius: 1)
                                    .animation(.spring, value: select)
                                    .onTapGesture {
                                        // 有则添加 无责删除 到 list
                                        toggle_select_of_memo(memo: memo)
                                    }
                            }
                        }
                    }
                })
                .padding()
                .animation(.bouncy, value: homeVM.trashMemos)
                
            }
            .navigationTitle("Trash")
            .navigationBarTitleDisplayMode(.inline)
            .animation(.bouncy, value: homeVM.trashMemos)
            .toolbar(content: {
                ToolbarItem(placement: .topBarTrailing) {
                    Button {
                        select.toggle()
                        selected_list.removeAll()
                    } label: {
                        Text("select")
                    }
                }
                
                if select, !selected_list.isEmpty {
                    ToolbarItem(placement: .bottomBar) {
                        Button {
                            homeVM.deleteMemos(memos: selected_list)
                            selected_list.removeAll()
                        } label: {
                            Image(systemName: "trash")
                        }
                    }
                    
                    ToolbarItem(placement: .bottomBar) {
                        Button {
                            homeVM.rollbackMemos(memos: selected_list)
                            selected_list.removeAll()
                        } label: {
                            Image(systemName: "shippingbox.and.arrow.backward.fill")
                        }
                    }
                }

            })
        }
    }
    
    private func isMemoInDel_list(memo: MemoEntity) -> Bool {
        let memo = selected_list.first(where: { $0.id == memo.id })
        
        if memo == nil {
            return false
        } else {
            return true
        }
    }
    
    // 有则添加 无责删除
    private func toggle_select_of_memo(memo: MemoEntity) {
        let selected = isMemoInDel_list(memo: memo)
        
        if selected {
            selected_list.removeAll { $0.id == memo.id }
        } else {
            selected_list.append(memo)
        }
    }
}

#Preview {
    TrashView()
        .environmentObject(HomeViewModel())
}
